package router

import (
	"dhuviewer/dto"
	"dhuviewer/jwt"
	"errors"

	"github.com/gin-gonic/gin"
)

func HelpcheckJWT(c *gin.Context) (dto.JwtPayload, error) {
	const BEARER_SCHEMA = "Bearer"
	authHeader := c.GetHeader("Authorization")
	if len(authHeader) < len(BEARER_SCHEMA)+1 {
		c.JSON(200, gin.H{
			"status": 40000,
		})
		return dto.JwtPayload{}, errors.New("没登陆")
	}
	tokenString := authHeader[len(BEARER_SCHEMA)+1:]

	res, payload := jwt.CheckJwt(tokenString)
	if !res {
		c.JSON(200, gin.H{
			"status": 40000,
		})
		return dto.JwtPayload{}, errors.New("没登陆")
	}
	return payload, nil
}
